package com.zhongjiananbang.textdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhongjiananbang.textdemo.adapter.LeftAdapter;
import com.zhongjiananbang.textdemo.model.RightModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private CommonPopupWindow popupWindow;
    private List<String> listLeft;
    private List<RightModel> listRight;
    private List<String> list=new ArrayList<>();
    private String []names={"北京","上海","深圳","广州","山东","济南","河北","河南","山西","陕西","东北","黑龙江","华北",
            "华北1","华北2","华北3","华北4","华55北","华66北","华777北"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setDatas();
        initView();
    }

    private void setDatas() {
        listLeft = new ArrayList<>();
        listRight = new ArrayList<>();
        for(int i=0;i<20;i++){
            listLeft.add("北京"+i);
            List list1=new ArrayList();
            for(int j=0;j<5;j++){
                list1.add(j+names[i]);
            }
            listRight.add(new RightModel(i,list1));
        }
    }

    //popuwindows向下弹出
    public void showDownPop(View view) {
        if (popupWindow != null && popupWindow.isShowing()) return;
        View viewDown = LayoutInflater.from(this).inflate(R.layout.popup_down, null);
        RecyclerView recyclerView_left=viewDown.findViewById(R.id.recyclerView_left);
        RecyclerView recyclerView_right=viewDown.findViewById(R.id.recyclerView_right);
        recyclerView_left.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView_right.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        LeftAdapter leftAdapter = new LeftAdapter(MainActivity.this, listLeft, true);
        recyclerView_left.setAdapter(leftAdapter);
        list.addAll(listRight.get(0).getList());
        final LeftAdapter rightAdapter = new LeftAdapter(MainActivity.this,list,false);
        recyclerView_right.setAdapter(rightAdapter);

        leftAdapter.setRecyclerViewListener(new RecyclerViewListener() {
            @Override
            public void onItemListner(int postion) {
                Log.d("zzzzzzzzzz", "onClick:回调了");
                list.clear();
                list.addAll(listRight.get(postion).getList());
                rightAdapter.notifyDataSetChanged();
            }
        });
        popupWindow = new CommonPopupWindow.Builder(this)
                .setView(viewDown)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.dip2px(MainActivity.this,272))
                .setAnimationStyle(R.style.AnimDown)
                .setBackGroundLevel(0.7f)//取值范围0.0f-1.0f 值越小越暗
//                .setViewOnclickListener(HomeActivity.this)
                .setOutsideTouchable(true)
                .create();
        popupWindow.showAsDropDown(view);
    }
    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDownPop(textView);
            }
        });
    }
}
